#include "adminoutsectform.h"
#include "ui_adminoutsectform.h"

//constructor to set up outline section form for admin
AdminOutSectForm::AdminOutSectForm(QWidget *parent, AdminData *newData) :
    QWidget(parent),
    ui(new Ui::AdminOutSectForm)
{
    ui->setupUi(this);

    this->data=newData;

    //changes when persistent

    this->oSects=(data->getOutlineSections());
    int i=0;
    while(i<oSects.size())
    {
        ui->textBrowser->append(oSects[i].getTitle());
        i++;
    }


    //show previously created holidays


}

//adminoutsectform destructor
AdminOutSectForm::~AdminOutSectForm()
{
    delete ui;
}


//method to show calendar page
void AdminOutSectForm::on_pushButton_clicked()
{
    AdminMain *aM=new AdminMain(this,data);
    aM->show();
    aM->topLevelWidget();
}


//method to show course form page
void AdminOutSectForm::on_pushButton_2_clicked()
{
    AdminCourseForm *aCF=new AdminCourseForm(this, data);
    aCF->show();

    aCF->topLevelWidget();
}


//method for adding a section
void AdminOutSectForm::on_pushButton_3_clicked()
{
    //get title
    QString outTitle=ui->lineEdit->text();

    //if no title, give error message
    if(outTitle.isEmpty() || outTitle.isNull())
    {

        AdminErrorDialog *aed32=new AdminErrorDialog(this,"Must have a title.");
        aed32->setModal(true);
        aed32->exec();

    }
    else
    {//get information

        QString sectOpOrMan=ui->comboBox_2->currentText();
        bool optTrue;
        if(sectOpOrMan.compare("Optional"))
        {
            optTrue=true;
        }
        else
        {
            optTrue=false;
        }

        QString descript=ui->plainTextEdit->toPlainText();
        QString checkUpDat=ui->comboBox->currentText();
        bool canChange=false;
        if(checkUpDat.compare("Optional"))
        {
            canChange=true;
        }
        QString defText=ui->plainTextEdit_2->toPlainText();
        bool needsDates=ui->checkBox->isChecked();



        //make section with this information
        OutlineSection *newOS=new OutlineSection(outTitle, descript,optTrue ,defText, canChange, needsDates);

        //check if section has been created already
        int i=0;
        bool isThere=false;
        while(i<oSects.size()&& isThere==false)
        {
            if(oSects[i].getTitle().compare(outTitle)==0)
            {
                isThere=true;
            }

            i++;
        }

        //if section is there already, give error message
        if(isThere==true)
        {
            ui->lineEdit->clear();

            AdminErrorDialog *aed33=new AdminErrorDialog(this,"Sorry "+outTitle+" is already there.");
            aed33->setModal(true);
            aed33->exec();
        }
        else
        {

            //add and show the section, then clear the fields
            ui->textBrowser->append(outTitle);
            oSects.prepend(*(newOS));
            ui->lineEdit->clear();
            ui->plainTextEdit->clear();
            ui->plainTextEdit_2->clear();

        }
    }
}



//method for deleting last section
void AdminOutSectForm::on_pushButton_4_clicked()
{
    //if no sections have been added yet, give an error message
    if(oSects.isEmpty())
    {
        ui->lineEdit->clear();

        AdminErrorDialog *aed34=new AdminErrorDialog(this,"Nothing to remove.");
        aed34->setModal(true);
        aed34->exec();
    }
    else
    {
        //otherwise, remove the section and redisplay the rest
        oSects.remove(0);
        ui->textBrowser->clear();
        int i=0;
        while(i<oSects.size())
        {
            ui->textBrowser->append(oSects[i].getTitle());
            i++;
        }
    }
}


//persistence method
void AdminOutSectForm::on_pushButton_5_clicked()
{
    data->setOutlineSections(&oSects);

    AdminErrorDialog *aedPer3=new AdminErrorDialog(this,"Data has been saved.");
    aedPer3->setModal(true);
    aedPer3->exec();
    //persistence stuff here, read in from QVector, possible deleting old

}


void AdminOutSectForm::on_pushButton_6_clicked()
{
    PrdfForm *PDF=new PrdfForm(this, data);
    PDF->show();
    PDF->topLevelWidget();
}
